const ProductService = require("../../services/admin/ProductService")

const ProductController = {
    add:async (req,res)=>{

        const cover = req.file ? `/productuploads/${req.file.filename}` : ""
        const {title,detail,introduction} = req.body
        await ProductService.add({
            title,
            detail,
            introduction,
            cover,
            editTime:new Date()
        })

        res.send({
            ActionType:"OK"
        })
    },

    getList:async (req,res)=>{
        const result = await ProductService.getList({_id:req.params.id})
        res.send({
            ActionType:"OK",
            data:result
        })
    },

    updateList:async (req,res)=>{
        const cover = req.file ? `/productuploads/${req.file.filename}` : ""
        const {title,detail,introduction,_id} = req.body
        await ProductService.updateList({
            _id,
            title,
            detail,
            introduction,
            cover,
            editTime:new Date()
        })

        res.send({
            ActionType:"OK"
        })
    },

    delList:async (req,res)=>{
        await ProductService.delList({_id:req.params.id})
        res.send({
            ActionType:"OK"
        })
    }
}

module.exports = ProductController